<template>
  <view>
    <button type="default" @tap="login">获取code信息</button>
  </view>
</template>

<script>
import { api } from "../../utils/api.js";
export default {
  data() {
    return {};
  },
  methods: {
    login() {
      let encryptedData, rawData, iv, signature, code;
      uni.login({
        //获取code
        provider: "weixin",
        onlyAuthorize: true, // 微信登录仅请求授权认证
        success: (res) => {
          code = res.code;
          uni.getUserInfo({
            provider: "weixin",
            success: (res) => {
              uni.setStorageSync("info", res.userInfo);
              console.log(res);
              encryptedData = res.encryptedData;
              rawData = res.rawData;
              iv = res.iv;
              signature = res.signature;
              uni.request({
                url: "https://api-hmugo-web.itheima.net/api/public/v1/users/wxlogin",
                method: "post",
                data: {
                  encryptedData,
                  rawData,
                  iv,
                  signature,
                  code,
                },
                success: (res) => {
                  // console.log(res)
                  uni.setStorageSync(
                    "token",
                    "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo"
                  );

                  uni.switchTab({
                    url: "/pages/myuser/myuser",
                  });
                },
              });
            },
          });
        },
      });
    },
  },
};
</script>

<style lang="scss">
</style>
